<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <link href="../../../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <link href="../../../jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css"
          rel="stylesheet"/>
    <script type="text/javascript" src="../../../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../../../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>

</head>
<body>

<div style="position:  relative; left: 30px;">
    <h3>新增字典值</h3>
    <div style="position: relative; top: -40px; left: 70%;">
        <button id="btnAdd" type="button" class="btn btn-primary">保存</button>
        <button type="button" class="btn btn-default" onclick="window.history.back();">取消</button>
    </div>
    <hr style="position: relative; top: -40px;">
</div>
<form class="form-horizontal" role="form">

    <div class="form-group">
        <label for="create-dicTypeCode" class="col-sm-2 control-label">字典类型编码<span style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <select class="form-control" id="create-dicTypeCode" style="width: 200%;">
                <option></option>
                <option>性别</option>
                <option>机构类型</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label for="create-dicValue" class="col-sm-2 control-label">字典值<span
                style="font-size: 15px; color: red;">*</span></label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="create-dicValue" style="width: 200%;">
        </div>
    </div>

    <div class="form-group">
        <label for="create-text" class="col-sm-2 control-label">文本</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="create-text" style="width: 200%;">
        </div>
    </div>

    <div class="form-group">
        <label for="create-orderNo" class="col-sm-2 control-label">排序号</label>
        <div class="col-sm-10" style="width: 300px;">
            <input type="text" class="form-control" id="create-orderNo" style="width: 200%;">
        </div>
    </div>
</form>

<div style="height: 200px;"></div>
</body>
</html>

<script>

    function loadType($select) {
        //清空option
        $select.empty();
        //加载type
        $.ajax({
            url: "/DictionaryType/getAll.action",
            type: "get",
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                console.log("准备开始请求：");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("请求失败：" + textStatus);
            },
            success: function (data, textStatus, jqXHR) {
                console.log("请求成功：");
                $(data).each(function (i, e) {
                    var temp = ' <option value="'+e.code+'" >' + e.name + '</option>';
                    $select.append(temp);
                });
            },
            complete: function (XMLHttpRequest, textStatus) {
                console.log("响应结果了：");
            }
        });


    }



    $(function () {

        loadType($("#create-dicTypeCode"));



        //添加事件
        $("#btnAdd").click(function () {

            //排序号的长度判定
            if ($("#create-orderNo").val().length<0 || $("#create-orderNo").val().length>7) {
                alert("排序号输入一个小于或者等于7位的整数");
                return;
            }

            //排序号的判定
            if (isNaN(parseInt($("#create-orderNo").val()))) {
                alert("排序号必须是一个整数");
                return;
            }


            $.ajax({
                url: "/DictionaryValue/add.action",
                type: "post",
                dataType: "json",
                data: {
                    value:$("#create-dicValue").val(),
                    text:$("#create-text").val(),
                    orderNo:$("#create-orderNo").val(),
                    typeCode:$("#create-dicTypeCode option:selected").val()
                },
                beforeSend: function (XMLHttpRequest) {
                    console.log("准备开始请求：");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("请求失败：" + textStatus);
                },
                success: function (data, textStatus, jqXHR) {
                    console.log("请求成功：");
                    if (data['result'] == "success") {

                        window.location="index.html";

                    } else {
                        window.alert(data['mess']);
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    console.log("响应结果了：");
                }
            });


        });



    });


</script>